package com.xy.service;

import com.xy.common.ResourceNotFoundException;
import com.xy.entity.UserProfilesEntity;
import com.xy.repo.UserProfileRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * .
 * <p>
 *
 * @author <a href="mailto:shunshun.lss@alibaba-inc.com">shunshun.lss</a>
 * @version 1.0.0
 * @since 2024-10-14
 */
@Service
public class UserProfileServiceImpl implements UserProfileService {

    private final UserProfileRepository userProfileRepository;

    @Autowired
    public UserProfileServiceImpl(UserProfileRepository userProfileRepository) {
        this.userProfileRepository = userProfileRepository;
    }

    @Override
    public UserProfilesEntity createUserProfile(UserProfilesEntity userProfilesEntity){
        return userProfileRepository.save(userProfilesEntity);
    }

    @Override
    public List<UserProfilesEntity> getAllUserProfiles() {
        return userProfileRepository.findAll();
    }

    @Override
    public Optional<UserProfilesEntity> getUserProfileById(UUID id) {
        return userProfileRepository.findById(id);
    }

    @Override
    public UserProfilesEntity updateUserProfile(UUID id, UserProfilesEntity userProfilesEntity) {
        UserProfilesEntity userProfile = userProfileRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("User profile not found"));

        userProfile.setFirstName(userProfilesEntity.getFirstName());
        userProfile.setLastName(userProfilesEntity.getLastName());
        userProfile.setEmail(userProfilesEntity.getEmail());

        return userProfileRepository.save(userProfile);
    }

    @Override
    public void deleteUserprofile(UUID id) {
        UserProfilesEntity userProfile = userProfileRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("User profile not found"));

        userProfileRepository.delete(userProfile);
    }
}